﻿@model Vacation.Models.ScoreModel

@{
    ViewBag.Title = "课程成绩";
}
<script src="@Url.Content("~/Scripts/MicrosoftAjax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/JsonSerialize.js")" type="text/javascript"></script>
<script type="text/javascript">

    $(document).ready(function () {
        var currentpage = 1;
        var pagecount = 1;

        //如果输入条件发生变化，当前页码标识放为1
        $("#studentno,#classno").bind("change", function (e) {
            currentpage = 1;
        });
        //自己输入的成绩查询
        $("#searchScoreSel").bind("click", function (e) {
            $("#myPnl").html("正在提交请求...");
            $.ajax({ url: '@Url.Action("searchScore", "Course")',
                type: 'POST',
                data: { "classno": $("#classno").val(), "studentno": $("#studentno").val(), "page": currentpage, "courseno": $("#courseno").text(), "teacher": 1 },
                timeout: 3000,
                error: function () { $("#myPnl").html("发生错误！"); },
                success: function (response) {
                    var tableinfo = "<table class=\"infolist\"><tr><td>学号</td><td>姓名</td><td>成绩</td><td>班级</td><td>输入时间</td></tr>";
                    for (var i = 0; i < response.studentscore.length; i++) {
                        tableinfo += "<tr>";
                        tableinfo += "<td>" + response.studentscore[i].StudentNo + "</td>";
                        tableinfo += "<td>" + response.studentscore[i].Name + "</td>";
                        tableinfo += "<td>" + response.studentscore[i].Score + "</td>";
                        tableinfo += "<td>" + response.studentscore[i].Class + "</td>";
                        tableinfo += "<td>" + response.studentscore[i].Date + "</td>";
                        tableinfo += "</tr>";
                    }
                    tableinfo += "</table>";
                    tableinfo += "<div id=\"pagebar\" class=\"paged\">";
                    pagecount = Math.ceil(response.total * 1.0 / response.pagesize);
                    tableinfo += "共找到" + response.total + "条记录,每" + response.pagesize + "页条,当前第<span id='page'>" + currentpage + "</span>页，共" + pagecount + "页 <a>第一页</a> | <a>上一页</a> | <a>下一页</a> | <a>最后页</a>";
                    tableinfo += "</div>";
                    $("#myPnl").html(tableinfo);


                    $("#pagebar a").attr("href", "#");
                    $("#pagebar a").bind("click", function (e) {

                        var index = $("#pagebar a").index(this);
                        // debugger;
                        switch (index) {
                            case 0: currentpage = 1; break;
                            case 1: if (currentpage - 1 > 0) currentpage--; break;
                            case 2: if (currentpage < pagecount) currentpage++; break;
                            case 3: currentpage = pagecount; break;
                            default: break;
                        }
                        $("#searchScoreSel").click()
                    });
                }
            });
        })

        //已输入成绩查询
        $("#searchScore").bind("click", function (e) {
            $("#myPnl").html("正在提交请求...");
            $.ajax({ url: '@Url.Action("searchScore", "Course")',
                type: 'POST',
                data: { "classno": $("#classno").val(), "studentno": $("#studentno").val(), "page": currentpage, "courseno": $("#courseno").text(), "teacher": 0 },
                timeout: 3000,
                error: function () { $("#myPnl").html("发生错误！"); },
                success: function (response) {
                    var tableinfo = "<table class=\"infolist\"><tr><td>学号</td><td>姓名</td><td>成绩</td><td>班级</td><td>教师</td><td>输入时间</td></tr>";
                    for (var i = 0; i < response.studentscore.length; i++) {
                        tableinfo += "<tr>";
                        tableinfo += "<td>" + response.studentscore[i].StudentNo + "</td>";
                        tableinfo += "<td>" + response.studentscore[i].Name + "</td>";
                        tableinfo += "<td>" + response.studentscore[i].Score + "</td>";
                        tableinfo += "<td>" + response.studentscore[i].Class + "</td>";
                        tableinfo += "<td>" + response.studentscore[i].Teacher + "</td>";
                        tableinfo += "<td>" + response.studentscore[i].Date + "</td>";
                        tableinfo += "</tr>";
                    }
                    tableinfo += "</table>";
                    tableinfo += "<div id=\"pagebar\" class=\"paged\">";
                    pagecount = Math.ceil(response.total * 1.0 / response.pagesize);
                    tableinfo += "共找到" + response.total + "条记录,每" + response.pagesize + "页条,当前第<span id='page'>" + currentpage + "</span>页，共" + pagecount + "页 <a>第一页</a> | <a>上一页</a> | <a>下一页</a> | <a>最后页</a>";
                    tableinfo += "</div>";
                    $("#myPnl").html(tableinfo);


                    $("#pagebar a").attr("href", "#");
                    $("#pagebar a").bind("click", function (e) {

                        var index = $("#pagebar a").index(this);

                        switch (index) {
                            case 0: currentpage = 1; break;
                            case 1: if (currentpage - 1 > 0) currentpage--; break;
                            case 2: if (currentpage < pagecount) currentpage++; break;
                            case 3: currentpage = pagecount; break;
                            default: break;
                        }
                        $("#searchScore").click()
                    });
                }
            });
        })

        //输入成绩处理
        $("#searchForInput").bind("click", function (e) {
            //var info = ;
            $("#myPnl").html("正在提交请求...");
            $.ajax({ url: '@Url.Action("searchStudent", "Course")',
                type: 'POST',
                //  data: { "data": String.toSerialize(info) },
                data: { "classno": $("#classno").val(), "studentno": $("#studentno").val(), "page": currentpage, "courseno": $("#courseno").text() },
                timeout: 3000,
                error: function () { $("#myPnl").html("发生错误！"); },
                success: function (response) {
                    // debugger;
                    //内容显示，列表+页码+提交按钮
                    var tableinfo = "<input id=\"updateInfo2\" type=\"button\" value=\"提交选中的成绩\"/>";
                    tableinfo += "<table class=\"infolist\"><tr><td><input id='selectAll' type='checkbox' /></td><td>学号</td><td>姓名</td><td>成绩</td><td>班级</td></tr>";
                    var scorelength = response.studentscore.length
                    for (var i = 0; i < scorelength; i++) {
                        tableinfo += "<tr>";
                        tableinfo += "<td><input name='checkbox' tabindex=2 type='checkbox' /></td><td><span name='student'>" + response.studentscore[i].StudentNo + "</span></td>";
                        tableinfo += "<td>" + response.studentscore[i].Name + "</td>";
                        var readonly = "";
                        if ($("#Normal").val() == true)
                            readonly = "readonly='readonly disabled='disabled''";
                        tableinfo += "<td><input name='score' " + readonly + " tabindex=1 class='score' type='text' value='" + $("#Normal").val() + "'/></td>";
                        tableinfo += "<td>" + response.studentscore[i].Class + "</td>";
                        tableinfo += "</tr>";
                    }
                    tableinfo += "</table>";
                    tableinfo += "<div id=\"pagebar\" class=\"paged\">";
                    pagecount = Math.ceil(response.total * 1.0 / response.pagesize);
                    tableinfo += "共找到" + response.total + "条记录,每" + response.pagesize + "页条,当前第<span id='page'>" + currentpage + "</span>页，共" + pagecount + "页 <a>第一页</a> | <a>上一页</a> | <a>下一页</a> | <a>最后页</a>";
                    tableinfo += "</div>";
                    tableinfo += "<input id=\"updateInfo\" type=\"button\" value=\"提交选中的成绩\"/>";
                    $("#myPnl").html(tableinfo);

                    //实现导航'
                    $("#pagebar a").attr("href", "#");
                    $("#pagebar a").bind("click", function (e) {

                        var index = $("#pagebar a").index(this);
                        // debugger;
                        switch (index) {
                            case 0: currentpage = 1; break;
                            case 1: if (currentpage - 1 > 0) currentpage--; break;
                            case 2: if (currentpage < pagecount) currentpage++; break;
                            case 3: currentpage = pagecount; break;
                            default: break;
                        }
                        $("#searchForInput").click()
                    });

                    //实现全选
                    $("#selectAll").bind("click", function (e) {
                        // debugger;
                        if ($("#selectAll").attr("checked"))
                            $("[name='checkbox']").attr("checked", 'true');
                        else
                            $("[name='checkbox']").removeAttr("checked");
                    });
                    //检查数据是否合理
                    $("[name='score']").bind("change", function (e) {
                        var str = $(this).val();
                        var reg = /^[0-9]{0,3}[.]{0,1}[0-9]{0,1}$/;
                        if (reg.exec(str) != null && Number(str) <= Number($("#total").text()) && Number(str) >= 0) {
                            var index = $("[name='score']").index(this);
                            $("[name='checkbox']:eq(" + index + ")").attr("checked", 'true');
                            $(this).removeClass("error");
                        }
                        else {
                            alert("必须为数字，最多保留1位小数，且小于总分" + $("#total").text());
                            $(this).addClass("error");

                        }
                    });
                    //绑定信息提交事件
                    $("#updateInfo,#updateInfo2").bind("click", function (e) {
                        var length = $("[name='checkbox']").length;
                        if ($("#school").text() != $("#TeacherSchool").val()) {
                            alert("您无法输入其他学院的课程成绩");
                            return;
                        }
                     //   debugger;
                        var count = 0;
                        var students = new Array();
                        for (var i = 0; i < length; i++) {
                            if ($("[name='checkbox']:eq(" + i + ")").attr("checked")) //如果选中
                            {
                                students[count] = new Object();
                                students[count].studentno = $("[name='student']:eq(" + i + ")").text();
                                var str = $("[name='score']:eq(" + i + ")").val();
                                //如果有一个不正常，就警告并退出函数
                                var reg = /^[0-9]{0,3}[.]{0,1}[0-9]{0,1}$/;
                                if (reg.exec(str) != null && Number(str) <= Number($("#total").text()) && Number(str) >= 0) {
                                    students[count].score = $("[name='score']:eq(" + i + ")").val();
                                }
                                else {
                                    alert("有不符合要求的成绩输入！请核对红色标记部分成绩");
                                    return false;
                                }
                                count++;
                            }
                        }
                        if (students.length == 0) {
                            alert("没有选中的记录！");
                            return;
                        }
                        if (!confirm("确定将这" + students.length + "个学生成绩提交么？\n一旦提交无法更改！"))
                            return;
                        var info = "";
                        info = { courseno: $("#courseno").text(), studentscore: students };
                        // debugger;
                        $.ajax({ url: '@Url.Action("Score", "Course")',
                            type: 'POST',
                            data: { "data": String.toSerialize(info) },
                            timeout: 3000,
                            error: function () { alert("发生错误！"); },
                            success: function (response) {
                                //  debugger;
                                if (response.length == 0) {
                                    alert("成功提交");
                                    currentpage = 1;
                                    $("#searchForInput").click();
                                }
                                else {
                                    var length = $("[name='student']").length;
                                    if (response == "您尚未登录") {
                                        alert(response);
                                        return;
                                    }
                                    for (var i = 0; i < length; i++)
                                        for (var j = 0; j < response.length; j++) {
                                            if ($("[name='student']:eq(" + i + ")").text() == response[j])
                                                $("[name='score']:eq(" + i + ")").addClass("error");
                                        }
                                    alert("有" + response.length + "人成绩没有正常输入。详情请查看红色标记部分。");
                                }
                            }
                        })
                    });

                }
            })
        });

    });
</script>
<h2>课程成绩</h2>
<p class="courseinfo">
@Html.LabelFor(model => model.Name):@Html.DisplayFor(model => model.Name) &nbsp; &nbsp;
@Html.LabelFor(model => model.CourseNo):<span id="courseno">@Html.DisplayFor(model => model.CourseNo)</span>&nbsp; &nbsp;
@Html.LabelFor(model => model.School):<span id="school">@Html.DisplayFor(model => model.School)</span>&nbsp; &nbsp;
@Html.LabelFor(model => model.Type):@Html.DisplayFor(model => model.Type) &nbsp; &nbsp;
@Html.LabelFor(model => model.Total):<span id="total">@Html.DisplayFor(model => model.Total)</span>
@Html.HiddenFor(model => model.NoScore)
@Html.HiddenFor(model => model.Normal)
@Html.HiddenFor(model=>model.TeacherSchool)
</p>
 <p>
        学号: @Html.TextBox("studentno", "%") 班号：@Html.TextBox("classno", "%") 
        <input id="searchForInput" type="button" value="检索学生"/>
        <input id="searchScore" type="button" value="查看所有已输入成绩" />
        <input id="searchScoreSel" type="button" value="查看本人输入成绩" />
        
</p>
<div id="myPnl"></div>
<p>说明：</p>
<p>1、检索学生时，本课程已有成绩记录的将不会出现在检索结果中。</p>
<p>2、输入成绩时，最多保留1位小数，且小于本课程的总分，出错时输入框为红色。</p>
<p>3、输入成绩时可以用“TAB”键切换到下一个学生，输入成绩后将自动选中该学生。只有选中的学生才能提交。</p>
<p>4、成绩一旦输入将无法更改。</p>
<p>5、类型为“讲座”、“社会实践”、“心理测评”、“职业测评”的课程分数默认为1分，不可更改，直接点打钩后提交即可。</p>
